﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace W1_MenuGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        
        List<VisibleGameObject> _gameMenu;
        VisibleGameObject _mnuNewGame;
        VisibleGameObject _mnuOptions;
        VisibleGameObject _mnuInfo;
        VisibleGameObject _mnuExit;

        VisibleGameObject _tempSpr;


        private int _idxSelectedMenu;

        Texture2D _backgroundMenu;

        // Keyboard states used to determine key presses
        KeyboardState _currentKeyboardState;
        KeyboardState _previousKeyboardState;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //Chỉnh size màn hình game
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 720;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Window.Title = "DEMO MENU GAME";
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            

            InitializeGameMenuScreen();
        }

        private void InitializeGameMenuScreen()
        {
            //Background
            _backgroundMenu = Content.Load<Texture2D>(@"Resources\Background\Background");
            
            //Menu
            _gameMenu = new List<VisibleGameObject>();
            List<KeyValuePair<string, int>> pathResouces;
            
            pathResouces = new List<KeyValuePair<string, int>>() 
            { new KeyValuePair<string, int>(@"Resources\Menu\NewGame", 1)};
            _mnuNewGame = new Menu();
            _mnuNewGame.Initialize(Content, pathResouces, new Vector2(50, 50));

            ((Menu)_mnuNewGame).IsSelected = true;
            _gameMenu.Add(_mnuNewGame);
            _idxSelectedMenu = 0;

            pathResouces = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>(@"Resources\Menu\Options", 1) };
            _mnuOptions = new Menu();
            _mnuOptions.Initialize(Content, pathResouces, new Vector2(50, 200));
            _gameMenu.Add(_mnuOptions);

            pathResouces = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>(@"Resources\Menu\Info", 1) };
            _mnuInfo = new Menu();
            _mnuInfo.Initialize(Content, pathResouces, new Vector2(50, 350));
            _gameMenu.Add(_mnuInfo);

            pathResouces = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>(@"Resources\Menu\ExitGame", 1) };
            _mnuExit = new Menu();
            _mnuExit.Initialize(Content, pathResouces, new Vector2(50, 500));
            _gameMenu.Add(_mnuExit);
            
            //Main Character
            pathResouces = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>(@"Resources\Menu\CharacterStand", 11),
            new KeyValuePair<string, int>(@"Resources\Menu\CharacterAttack", 23)};

            _tempSpr = new TempCharacter();
            _tempSpr.Initialize(Content, pathResouces, new Vector2(300, 100));
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            _previousKeyboardState = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();

            HandleSelectingMenu();

            foreach (VisibleGameObject menu in _gameMenu)
            {
                menu.Update(gameTime);
            }
            _tempSpr.Update(gameTime);

            base.Update(gameTime);
        }

        private void HandleSelectingMenu()
        {
            ((Menu)(((List<VisibleGameObject>)_gameMenu)[_idxSelectedMenu])).IsSelected = false;
            if (_currentKeyboardState.IsKeyDown(Keys.Down) && _previousKeyboardState.IsKeyUp(Keys.Down))
            {
                _idxSelectedMenu = ++_idxSelectedMenu % _gameMenu.Count;

                _tempSpr.CurrentSprite = (int)TempCharacter.TempCharacterAction.Dancing;
                _tempSpr.Sprites[_tempSpr.CurrentSprite].IsLooping = false;
            }
            else
            {
                if (_currentKeyboardState.IsKeyDown(Keys.Up) && _previousKeyboardState.IsKeyUp(Keys.Up))
                {
                    _tempSpr.CurrentSprite = (int)TempCharacter.TempCharacterAction.Dancing;
                    _tempSpr.Sprites[_tempSpr.CurrentSprite].IsLooping = false;

                    _idxSelectedMenu--;
                    if (_idxSelectedMenu < 0)
                    {
                        _idxSelectedMenu = _gameMenu.Count - 1;
                    }
                }
            }
            ((Menu)(((List<VisibleGameObject>)_gameMenu)[_idxSelectedMenu])).IsSelected = true;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            spriteBatch.Draw(_backgroundMenu, Vector2.Zero, Color.White);

            foreach (VisibleGameObject menu in _gameMenu)
            {
                menu.Draw(gameTime, spriteBatch);
            }

            _tempSpr.Draw(gameTime, spriteBatch);
            spriteBatch.End();



            base.Draw(gameTime);
        }

    }
}
